package com.xneure.tools;

import com.sun.tools.javap.JavapTask;
import com.sun.tools.javap.JavapTask.BadArgs;
import com.xneure.utils.FileUtil;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.bean.StringPrintWriter;

public class Javap{
	JavapTask javapTask = new JavapTask();
	StringPrintWriter newPrintWriter = StringPrintWriter.newPrintWriter();
	private String[] option = {"-p"};

	public Javap(){
		javapTask.setLog(newPrintWriter);
	}

	public void setOptions(String[] option){
		this.option = option;
	}

	public String decompilation(Class<?> cls){
		return decompilation(FileUtil.getWebPath(cls));
	}

	public String decompilation(String path){
		try{
			javapTask.handleOptions(ObjectUtil.arrayJoin(option,path));
			javapTask.call();
			return newPrintWriter.getValue();
		}catch(BadArgs e){
			return null;
		}
	}
}
